AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )

include('shared.lua')

local Ground = 1 + 0 + 2 + 8 + 32
local Pressure_Increment = 80
local Energy_Increment = 10

function ENT:Initialize()
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )
	
	local phys = self.Entity:GetPhysicsObject()
	self.val1 = 0
	self.overdrive = 0
	self.lastused = 0
	self.damaged = 0
	
	self.timer = CurTime() +  1
	if (phys:IsValid()) then
		phys:Wake()
		phys:SetMass(150)
	end
	
end

function ENT:Setup()
end

function ENT:Damage()
	if (self.damaged == 0) then
		self.damaged = 1
	end
end

function ENT:Repair()
	self.Entity:SetColor(255, 255, 255, 255)
	self.health = self.max_health
	self.damaged = 0
end

function ENT:Destruct()
	local Effect = EffectData()
		Effect:SetOrigin(self.Entity:GetPos())
		Effect:SetScale(1)
		Effect:SetMagnitude(25)
		util.Effect("Explosion", Effect, true, true)

		self.Entity:Remove()
end

function ENT:OnRemove()
	Dev_Unlink_All(self.Entity)
end


function ENT:Use()
end

function ENT:Output()
	return 1
end

function ENT:Collect_Hydrogen()
	local vel = self.Entity:WorldToLocal(self.Entity:GetVelocity()+self.Entity:GetPos())
	local speed = vel.z
	local square = false
	speed = speed/-1 --inverse it
	if (speed < 0) then speed = 0 end --make sure its not moving backwards
	local hydrogen = math.Round(speed/8)
	if (speed > 250) then 
		speed = speed - 250
		square = true
	end
	local hydrogen = math.Round(speed/8)
	if square then
		hydrogen = hydrogen + math.Round(math.sqrt(speed))
	end
	if (hydrogen > 35) then hydrogen = 35 end --maximum
	RD_SupplyResource(self, "hydrogen", hydrogen)
	self.val1 = speed
	return
end

function ENT:Think()
	if ( self.timer > CurTime() ) then return end
	self:Collect_Hydrogen()
	self.timer = CurTime() +  1
	self:SetOverlayText("Hydrogen Ramscoop\nGeneration: "..math.Round(self.val1/8))
end


function ENT:AcceptInput(name,activator,caller)
end

function ENT:PreEntityCopy()
	RD_BuildDupeInfo(self.Entity)
	if (WireAddon == 1) then
		local DupeInfo = Wire_BuildDupeInfo(self.Entity)
		if DupeInfo then
			duplicator.StoreEntityModifier( self.Entity, "WireDupeInfo", DupeInfo )
		end
	end
end

function ENT:PostEntityPaste( Player, Ent, CreatedEntities )
	RD_ApplyDupeInfo(Ent, CreatedEntities)
	if (WireAddon == 1) then
		if (Ent.EntityMods) and (Ent.EntityMods.WireDupeInfo) then
			Wire_ApplyDupeInfo(Player, Ent, Ent.EntityMods.WireDupeInfo, function(id) return CreatedEntities[id] end)
		end
	end
end
